﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Web.TestScripts;
using DevExpress.Web.ASPxEditors;
using System.Web.UI.WebControls;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Localization;
using DevExpress.Utils;
using DevExpress.Web.ASPxClasses;
using System.Web;

namespace SpeedSCM.Module.Web
{
    [PropertyEditor(typeof(string), false)]
    public class ActionsASPxPropertyEditor : ASPxPropertyEditor, IComplexPropertyEditor
    {
        private string predefinedValues;
        public ActionsASPxPropertyEditor(Type objectType, IModelMemberViewItem model)
            : base(objectType, model)
        {
        }

        public override void BreakLinksToControl(bool unwireEventsOnly)
        {
            if (this.ASPxComboBoxEditor != null)
            {
                this.ASPxComboBoxEditor.TextChanged -= new EventHandler(this.ExtendedEditValueChangedHandler);
            }
            base.BreakLinksToControl(unwireEventsOnly);
        }

        protected override WebControl CreateEditModeControlCore()
        {
            StringBuilder sb = new StringBuilder();
            if (_application != null)
            {
                foreach (var item in _application.Model.ActionDesign.Actions)
                {
                    sb.Append(item.Id).Append(";");
                }
                predefinedValues = sb.ToString().TrimEnd(';');
                base.Model.PredefinedValues = predefinedValues;
            }

            ASPxComboBox comboBox = RenderHelper.CreateASPxComboBox();
            this.SetupComboBox(comboBox);

            comboBox.TextChanged += new EventHandler(this.ExtendedEditValueChangedHandler);
            return comboBox;
        }

        protected override string GetPropertyDisplayValue()
        {
            return base.GetFormattedValue();
        }

        private void SetupComboBox(ASPxComboBox comboBox)
        {
            comboBox.DropDownStyle = DropDownStyle.DropDown;
            comboBox.AutoPostBack = base.ImmediatePostData;
            comboBox.MaxLength = base.MaxLength;
            foreach (string str in PredefinedValuesEditorHelper.CreatePredefinedValuesFromString(this.predefinedValues))
            {
                comboBox.Items.Add(str);
            }
        }

        // Properties
        public ASPxComboBox ASPxComboBoxEditor
        {
            get
            {
                return (ASPxComboBox)base.Editor;
            }
        }

        private XafApplication _application;
        public void Setup(DevExpress.ExpressApp.IObjectSpace objectSpace, DevExpress.ExpressApp.XafApplication application)
        {
            if (_application == null)
            {
                _application = application;
            }
        }
    }
}
